package com.sgb.kjwl.view.ui.activity.packers;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityFindPacksBinding;
import com.sgb.kjwl.model.entity.FindPackersEntity;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.utils.NumberConstant;
import com.sgb.kjwl.view.ui.adapter.packers.FindPackersAdapter;
import com.sgb.kjwl.view.widget.PickPopWindow1;
import com.sgb.kjwl.view.widget.ThreeLevelAreaWindow;
import com.sgb.kjwl.view.widget.ThreeLevelFilterWindow;
import com.sgb.kjwl.viewmodel.packers.FindPackersAtvModel;
import com.sgb.link.router.PathTeam;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.common.utils.ActivityJump;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import static com.sgb.kjwl.utils.NumberConstant.TEAM_CONSTRUCTION;

/**
 * description:找工队
 * @author 陈方凯
 * @date 2020/6/15
 */
public class FindPackersActivity extends AppActivity {

    public static final int TEAM_GROUP = 1;

    private int currentType;
    private String mWorkOne,mWorkTwo,mWorkThree;

    public FindPackersAtvModel viewModel;

    private ActivityFindPacksBinding binding;
    private FindPackersAdapter findPackersAdapter;

    /**
     * 工程分类
     */
    private ThreeLevelFilterWindow popCategory;
    private PickResultEntity pickResultProject1;
    private PickResultEntity pickResultProject2;
    private PickResultEntity pickResultProject3;
    /**
     * 团队规模
     */
    private PickPopWindow1 popTeamSize;
    private PickResultEntity pickResultTeamSize;
    /**
     * 所在区域
     */
    private ThreeLevelAreaWindow popCity;
    private PickResultEntity pickResultProvince;
    private PickResultEntity pickResultCity;
    private PickResultEntity pickResultArea;

    private String publishTime;

    public static void start(Context context, int type) {
        Intent intent = new Intent();
        intent.putExtra("type", type);
        intent.setClass(context, FindPackersActivity.class);
        context.startActivity(intent);
    }

    public static void start(Context context, int type,String no) {
        Intent intent = new Intent();
        intent.putExtra("type", type);
        intent.setClass(context, FindPackersActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    private void initFilterData(boolean isLoadMore) {

        viewModel.getNewList(getString(R.string.app_all).equals(pickResultTeamSize.getName())?"":pickResultTeamSize.getName(), pickResultProject1.getNo(),
                pickResultProject2.getNo(), pickResultProject3.getNo(), currentType + "",
                pickResultProvince.getNo(), pickResultCity.getNo(), pickResultArea.getNo(), isLoadMore,publishTime);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(mActionBar!=null) {
            if (currentType == TEAM_CONSTRUCTION) {
                mActionBar.setTitle("施工队");
            }
            if (currentType == TEAM_GROUP) {
                mActionBar.setTitle("班组工队");
            }
        }
    }

    @Override
    protected void initView() {
        if(viewModel==null){
            viewModel = new ViewModelProvider(this).get(FindPackersAtvModel.class);
        }
        currentType = getIntent().getIntExtra("type", TEAM_CONSTRUCTION);
        mWorkOne = getIntent().getStringExtra("workKindOne");
        mWorkTwo = getIntent().getStringExtra("workKindTwo");
        mWorkThree = getIntent().getStringExtra("workKindThree");
        publishTime = getIntent().getStringExtra("publishTime");

        binding = DataBindingUtil.setContentView(this, R.layout.activity_find_packs);

        if (findPackersAdapter == null) {
            if (currentType == TEAM_CONSTRUCTION) {
                findPackersAdapter = new FindPackersAdapter(getActivity(), null, Glide.with(this),true);
            } else  {
                findPackersAdapter = new FindPackersAdapter(getActivity(), null, Glide.with(this),false);
            }
        }
        findPackersAdapter.setOnItemClick(new AdapterOnItemClick<FindPackersEntity>() {
            @Override
            public void onItemClick(FindPackersEntity findPackersEntity, int position) {
                viewModel.getH5Url(findPackersEntity.getNo());
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(findPackersAdapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initFilterData(false);
            }

            @Override
            public void onLoadMore() {
                initFilterData(true);
            }
        });

        pickResultProject1 = new PickResultEntity();
        pickResultProject2 = new PickResultEntity();
        pickResultProject3 = new PickResultEntity();
        pickResultTeamSize = new PickResultEntity();
        pickResultProvince = new PickResultEntity();
        pickResultCity = new PickResultEntity();
        pickResultArea = new PickResultEntity();

        if (!TextUtils.isEmpty(mWorkOne)){
            pickResultProject1.setNo(mWorkOne);
        }

        if (!TextUtils.isEmpty(mWorkTwo)){
            pickResultProject2.setNo(mWorkTwo);
            pickResultProject3.setNo(mWorkThree);
        }

    }

    @Override
    protected void init() {
        initFilterData(false);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_project) {
            if (popCategory == null) {
                viewModel.getCategoryType();
            } else {
                showCategoryPop(null);
            }
        } else if (id == R.id.tv_team_size) {
            if (popTeamSize == null) {

            } else {
                showTeamSizePopWindow(null);
            }
        } else if (id == R.id.tv_area) {
            if (popCity == null) {
                viewModel.getCityList();
            } else {
                showCityPop(null);
            }
        }
    }

    public void showList(List<FindPackersEntity> list, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                findPackersAdapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                findPackersAdapter.setDatas(list);
                binding.recyclerView.smoothScrollToPosition(0);
                if (list.size() < NumberConstant.NUMBER_TEN) {
                    binding.recyclerView.setNoMore(true);
                }
            } else {
                findPackersAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * 工程-工程分类
     * @param data 工程分类的popWindow的数据
     */
    public void showCategoryPop(List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> data) {
        if (popCategory == null) {
            popCategory = new ThreeLevelFilterWindow(getActivity());
            popCategory.setBgView(binding.grayLayout);
            popCategory.setOnResultClick(new ThreeLevelFilterWindow.OnResultClick() {
                @Override
                public void result(Object key1, Object key2, Object key3) {
                    pickResultProject1.setNo(((PickResultEntity) key1).getNo());
                    pickResultProject1.setName(((PickResultEntity) key1).getName());
                    pickResultProject2.setNo(((PickResultEntity) key2).getNo());
                    pickResultProject2.setName(((PickResultEntity) key2).getName());
                    pickResultProject3.setNo(((PickResultEntity) key3).getNo());
                    pickResultProject3.setName(((PickResultEntity) key3).getName());
                    initFilterData(false);
                }

                @Override
                public void reset() {
                    pickResultProject1.clear();
                    pickResultProject2.clear();
                    pickResultProject3.clear();
                    initFilterData(false);
                }
            });
            popCategory.setOnDismissListener(() -> {
                if (TextUtils.isEmpty(pickResultProject1.getNo())) {
                } else {
                }
            });

            if (!"".equals(pickResultProject1.getNo())){
                popCategory.setThreeLevelDataForNormal(data,mWorkOne,mWorkTwo,mWorkThree);
            }else {
                popCategory.setThreeLevelData(data);
            }
        }
    }

    //团队规模pop
    public void showTeamSizePopWindow(List<PickSelectEntity> data) {
        if (popTeamSize == null) {
            popTeamSize = new PickPopWindow1(this);
            popTeamSize.setBgView(binding.grayLayout);
            popTeamSize.setOnResultClick((PickPopWindow1.OnResultClick) (key1, key2, key3) -> {
                pickResultTeamSize.setNo((String) key1);
                pickResultTeamSize.setName((String) key2);
                pickResultTeamSize.setPosition((int) key3);
                if (TextUtils.isEmpty((String) key2) || TextUtils.equals(getString(R.string.app_all), (String) key2)) {

                } else {

                }
                initFilterData(false);
            });
            popTeamSize.setOnDismissListener(() -> {
            });

            popTeamSize.setData(data);
        }
    }

    /**
     * 省市区pop
     * @param data 省市区pop的数据
     */
    public void showCityPop(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        if (popCity == null) {
            popCity = new ThreeLevelAreaWindow(getActivity()){
                @Override
                protected void provinceClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.provinceClick(baseAreaEntity, position);
                    pickResultProvince.setNo(baseAreaEntity.getNo());
                    pickResultProvince.setName(baseAreaEntity.getSimpleName());
                    if (getString(R.string.app_all).equals(baseAreaEntity.getSimpleName()) || pickResultProvince.getName().equals(baseAreaEntity.getSimpleName())){
                        pickResultCity.setNo("");
                        pickResultCity.setName("");
                    }
                }

                @Override
                protected void cityClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.cityClick(baseAreaEntity, position);
                    pickResultCity.setNo(baseAreaEntity.getNo());
                    pickResultCity.setName(baseAreaEntity.getSimpleName());
                    if (getString(R.string.app_all).equals(baseAreaEntity.getSimpleName()) || pickResultCity.getName().equals(baseAreaEntity.getSimpleName())){
                        pickResultArea.setNo("");
                        pickResultArea.setName("");
                    }
                }

                @Override
                protected void areaClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.areaClick(baseAreaEntity, position);
                    pickResultArea.setNo(baseAreaEntity.getNo());
                    pickResultArea.setName(baseAreaEntity.getSimpleName());
                }
            };
            popCity.setBgView(binding.grayLayout);
            popCity.setProvinceSingle();
            popCity.setCitySingle();
            popCity.setAreaSingle();
            popCity.setShowSimpleName(true);
            popCity.setOnResultClick((key1, key2, key3) -> {
                initFilterData(false);
            });
            popCity.setOnDismissListener(() -> {
                if (TextUtils.isEmpty(pickResultProvince.getNo())) {
                } else {
                }
            });

        }
    }

    public void getUrl(String url,String no) {
        ActivityJump.startActivity(PathTeam.TeamDetailActivity, Pair.create("no",no),Pair.create("url",url));
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (NumberConstant.sIsBackForTeam == 1){
            initFilterData(false);
            NumberConstant.sIsBackForTeam = 0;
        }
    }
}
